package com.xingmang.common.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.xingmang.common.enums.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Response;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther: hansom
 * @Version: 1.0
 * @create 2023/4/18 13:37
 */
@Configuration
@EnableOpenApi
@EnableKnife4j
public class Knife4jConfig {
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .groupName("后台管理")
                .select()
                //.paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.any())
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build()
                .directModelSubstitute(Timestamp.class, Long.class)
                .apiInfo(apiInfo())
                .ignoredParameterTypes(HttpServletRequest.class, HttpServletResponse.class)
                .globalResponses(HttpMethod.GET, getResponseMessage())
                .globalResponses(HttpMethod.POST, getResponseMessage());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("温州城市大学接口文档")
                .description("温州城市大学后台开发文档")
                .version("1.0.0")
                .termsOfServiceUrl("http://localhost:8882/admin")
                .build();
    }

    private List<Response> getResponseMessage() {
        List<Response> resMsgList = new ArrayList<>();
        for (ResultCode item : ResultCode.values()) {
            resMsgList.add(new ResponseBuilder().code(item.getCode()+"").description(item.getMsg()).build());
        }
        return resMsgList;
    }
}
